//
//  RegisterViewController.m
//  JianChaRiBao
//
//  Created by Changxy on 13-6-4.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "RegisterViewController.h"
#import "UserAgreementViewController.h"
#import "AppDelegate.h"
@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerSuccess:) name:kRegisterSuccess object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerFail:) name:kRegisterFail object:nil];
        
    }
    return self;
}
-(void)registerSuccess:(NSNotification *)notification{
    [self removeloadingView];
    ABPromptView * abp = [[[ABPromptView alloc]initWithTitle:@"注册成功"]autorelease];
    [abp show];
    [[AuthorManager defaultManager] loginWithUsername:usernameText.text password:passwordText.text];

    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)registerFail:(NSNotification *)notification{
    [self removeloadingView];
    ABPromptView * abp = [[[ABPromptView alloc]initWithTitle:[[AuthorManager defaultManager].userInfo objectForKey:@"error"]]autorelease];
    [abp show];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    shouldSwipeBack = YES;

}
- (IBAction)backBtnClicked:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)agreeBtnClicked:(id)sender {
    agreeBtn.selected = !agreeBtn.selected;
    registerBtn.enabled = agreeBtn.selected;
}
- (IBAction)userDelegateBtnClicked:(id)sender {
    UserAgreementViewController * userView = [[UserAgreementViewController alloc]initWithNibName:@"UserAgreementViewController" bundle:nil];
    [[UIApplication rootNavController]pushViewController:userView animated:YES];
    [userView release];
    
}
//正则表达式
-(BOOL)isValidateEmail:(NSString *)email

{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
    
    return [emailTest evaluateWithObject:email];
}
-(BOOL)isValidateUsername:(NSString *)username

{
    NSString * usernameRegex = @"[A-Z0-9a-z]{5,20}";
    
    NSPredicate *userText = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",usernameRegex];
    
    return [userText evaluateWithObject:username];
}

-(BOOL)isValidatePassworld:(NSString *)passworld{
    NSString * usernameRegex = @"[A-Z0-9a-z]{6,16}";
    
    NSPredicate *userText = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",usernameRegex];
    
    return [userText evaluateWithObject:passworld];
}

- (IBAction)registerBtnClicked:(id)sender {
    if (usernameText.text == nil || [usernameText.text isEqualToString:@""] || emailText.text == nil || [emailText.text isEqualToString:@""] || passwordText.text == nil || [passwordText.text isEqualToString:@""] || passwordText2.text == nil || [passwordText2.text isEqualToString:@""]) {
        ABPromptView * abp = [[[ABPromptView alloc]initWithTitle:@"资料不完整"]autorelease];
        [abp show];
        usernameText.text = nil;
    }else if(![self isValidateUsername:usernameText.text]){
        ABPromptView * abp = [[[ABPromptView alloc]initWithTitle:@"用户名非法"]autorelease];
        [abp show];
        usernameText.text = nil;
    }else if(![self isValidateEmail:emailText.text]){
        ABPromptView * abp = [[[ABPromptView alloc]initWithTitle:@"邮箱格式错误"]autorelease];
        [abp show];
        emailText.text = nil;
    }else if(![self isValidatePassworld:passwordText.text]){
        ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"输入密码非法"];
        [abp show];
        [abp release];
        
    }else if(![passwordText.text isEqualToString:passwordText2.text]){
        ABPromptView * abp = [[[ABPromptView alloc]initWithTitle:@"两次密码不一致"]autorelease];
        [abp show];
        
    }else{
        [self showLoadingView];
        [[AuthorManager defaultManager] registerWithUsername:usernameText.text password:passwordText.text email:emailText.text];
    }

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [usernameText resignFirstResponder];
    [emailText resignFirstResponder];
    [passwordText resignFirstResponder];
    [passwordText2 resignFirstResponder];
    return YES;
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:self.view.bounds] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [agreeBtn release];
    [usernameText release];
    [emailText release];
    [passwordText release];
    [passwordText2 release];
    [registerBtn release];
    [super dealloc];
}
- (void)viewDidUnload {
    [agreeBtn release];
    agreeBtn = nil;
    [usernameText release];
    usernameText = nil;
    [emailText release];
    emailText = nil;
    [passwordText release];
    passwordText = nil;
    [passwordText2 release];
    passwordText2 = nil;
    [registerBtn release];
    registerBtn = nil;
    [super viewDidUnload];
}
@end
